﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MyStream.Core;

namespace MyStream.UI
{
    public partial class frmOptions : Form
    {
        private frmMain _parent;
        private Options _loadedOptions;
        private Options _currentOptions;

        public frmOptions(frmMain parent)
        {
            InitializeComponent();
            _parent = parent;
            _loadedOptions = parent.CurrentOptions;
            _currentOptions = _loadedOptions;
            SetPreviousValues();
        }

        private void LoadWorkSpaces()
        {
            foreach (string wsName in VersionControlService.GetWorkSpace())
            {
                
                bool isSelected = false;
                foreach (WorkspaceInfo wsi in _currentOptions.WorkSpaces)
                {
                    isSelected =
                        wsi.workspaceName == wsName.Split(new string[]{" on "}, StringSplitOptions.RemoveEmptyEntries)[0]
                        &&
                        wsi.serverName == wsName.Split(new string[]{" on "}, StringSplitOptions.RemoveEmptyEntries)[1]
                      ;
                }
                chkListWorkSpaces.Items.Add(wsName, isSelected);
            }
        }

        private void SetPreviousValues()
        {
            LoadWorkSpaces();
            this.tbInterval.Value = _currentOptions.Interval;
            this.lbIntervalSelection.Text = _currentOptions.Interval + " minutes";

            this.tbDeletionInterval.Value = _currentOptions.Interval;
            this.lbIntervalSelection.Text = _currentOptions.Interval + " hours";

            this.txtMark.Text = _currentOptions.CommentMarkPattern;
            
        }

        private void tbInterval_Scroll(object sender, EventArgs e)
        {
            this.lbIntervalSelection.Text = this.tbInterval.Value + " minutes";
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            List<WorkspaceInfo> newWorkspaces = new List<WorkspaceInfo>();
            foreach (string s in chkListWorkSpaces.CheckedItems)
            {
                WorkspaceInfo wsi = new WorkspaceInfo();
                wsi.serverName = s.Split(new string[]{" on "},StringSplitOptions.RemoveEmptyEntries)[1];
                wsi.workspaceName = s.Split(new string[] { " on " }, StringSplitOptions.RemoveEmptyEntries)[0];
                newWorkspaces.Add(wsi);

                _currentOptions.WorkSpaces = newWorkspaces;
            }

            _currentOptions.Interval = this.tbInterval.Value;
            _currentOptions.MaxDuration = this.tbDeletionInterval.Value;
            _currentOptions.CommentMarkPattern = this.txtMark.Text;
            _parent.CurrentOptions = _currentOptions;
            
            this.Close();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tbDeletionInterval_Scroll(object sender, EventArgs e)
        {
            this.lbDeletionIntervalSelection.Text = this.tbDeletionInterval.Value + " hours";
        }
    }
}
